<?php  

session_start();
require_once('connect.php');
require_once('includes/user.class.php');
require_once('includes/rooms.class.php');

$database = Database::getInstance();
$rooms = new Rooms();
$user = new User();

if(!$user->is_logged()) header('Location: index.php');

require_once('partials/header.php'); //wczytaj header i menu
?>


    <div class="container">

        <?php
        //wyświetl informację o logowaniu jeśli występuje
        if(isset($_SESSION['alert'])){
            echo '<div class="info">'.$_SESSION['alert'].'</div>';
        }
        ?>

        <div class="row">

            <div class="col-md-3">

                <?php require_once('partials/content-left-menu.php'); ?>

            </div>

            <div class="col-md-9">
				<div class="subject-table">
					<div class="subject-table-header subject-table-row">
						<div class="subject-table-header-cell">Budynek</div>
						<div class="subject-table-header-cell">Sala</div>
						<div class="subject-table-header-cell">Ilość miejsc</div>
					</div>
					<?php
					$roomsData = $rooms -> get_rooms();
					foreach($roomsData as $room){
						print '<div class="subject-table-row">';
						print'<div class="subject-table-header-cell">'.$room['building'].'</div>';
						print'<div class="subject-table-header-cell">'.$room['number'].'</div>';
						print'<div class="subject-table-header-cell">'.$room['capacity'].'</div>';
						print '</div>';
					}
					?>
				</div>
				<!--TYLKO DLA ADMINOW -->
				<?php 
				if(isset($_GET['addRoom'])){
					$building = $_GET['building'];
					$number = $_GET['number'];
					$capacity = $_GET['capacity'];
							
					if($rooms->check_room($building,$number)){
						$add = $rooms->add_room($building,$number,$capacity);
						if($add > 0){
							header( 'Location:rooms.php');
						}else{
							$alert = "Nie dodano salia";
						}
					}else{
						$alert = "Taka sala już istnieje";
					}
				print $alert;
				}
				?>
				<div class="add-news-subject-area">
				<form action = "rooms.php" method = "GET">
					<input type="text" name="building" id="LOGIN" class="subject-input" placeholder="Budynek">
					<input type="text" name="number" id="LOGIN" class="subject-input" placeholder="Sala">
					<input type="text" name="capacity" id="LOGIN" class="subject-input" placeholder="Ilość miejsc">
					<input type="hidden" name="addRoom" value="true">
					<input type="submit" name="submit" value="Zapisz salę w bazie" class="subject-button">
				</form>
				</div>
					<!--TYLKO DLA ADMINOW -->
            </div>


                </div>

            </div>

        </div>

    </div>
 
<?php
require_once('partials/footer.php');
?>
